﻿#include "GameshopDialog.h"
#include "../GameScene.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../MirSounds/SoundManager.h"
#include "../../Settings.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

    GameShopDialog::GameShopDialog()
    {
        GameScene::GameShopInfoList.clear();
        setIndex(749);
        setLibrary(Libraries::Title);
        setMovable(true);
        setLocation(getCenter());
        setSort(true);

        TitleLabel = new MirImageControl();
        TitleLabel->setIndex(26);
        TitleLabel->setLibrary(Libraries::Title);
        Point tempVar(18, 9);
        TitleLabel->setLocation(&tempVar);
        TitleLabel->setParent(this);

        Grid = std::vector<GameShopCell*>(4 * 2);
        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                int idx = 4 * y + x;
                GameShopCell *tempVar2 = new GameShopCell();
                Size tempVar3(125, 146);
                tempVar2->setSize(&tempVar3);
                tempVar2->setVisible(true);
                tempVar2->setParent(this);
                Grid[idx] = tempVar2;

                delete tempVar2;
            }
        }

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar4(671, 4);
        CloseButton->setLocation(&tempVar4);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

        totalGold = new MirLabel();
        Size tempVar5(100, 20);
        totalGold->setSize(&tempVar5);
        totalGold->setDrawFormat(TextFormatFlags::RightToLeft | TextFormatFlags::Right);
        Point tempVar6(123, 449);
        totalGold->setLocation(&tempVar6);
        totalGold->setParent(this);
        totalGold->setNotControl(true);
        Font tempVar7(Settings::FontName, 8.0F);
        totalGold->setFont(&tempVar7);
        totalCredits = new MirLabel();
        Size tempVar8(100, 20);
        totalCredits->setSize(&tempVar8);
        totalCredits->setDrawFormat(TextFormatFlags::RightToLeft | TextFormatFlags::Right);
        Point tempVar9(5, 449);
        totalCredits->setLocation(&tempVar9);
        totalCredits->setParent(this);
        totalCredits->setNotControl(true);
        Font tempVar10(Settings::FontName, 8.0F);
        totalCredits->setFont(&tempVar10);

        UpButton = new MirButton();
        UpButton->setIndex(197);
        UpButton->setHoverIndex(198);
        UpButton->setPressedIndex(199);
        UpButton->setLibrary(Libraries::Prguse2);
        UpButton->setParent(this);
        Size tempVar11(16, 14);
        UpButton->setSize(&tempVar11);
        Point tempVar12(120, 103);
        UpButton->setLocation(&tempVar12);
        UpButton->setSound(SoundList::ButtonA);
        UpButton->setVisible(true);
        UpButton->Click += [&] (o, e)
        {
            if (CStartIndex <= 0)
            {
                return;
            }
    
            CStartIndex--;
    
            SetCategories();
            UpdatePositionBar();
        };

        DownButton = new MirButton();
        DownButton->setIndex(207);
        DownButton->setHoverIndex(208);
        DownButton->setLibrary(Libraries::Prguse2);
        DownButton->setPressedIndex(209);
        DownButton->setParent(this);
        Size tempVar13(16, 14);
        DownButton->setSize(&tempVar13);
        Point tempVar14(120, 421);
        DownButton->setLocation(&tempVar14);
        DownButton->setSound(SoundList::ButtonA);
        DownButton->setVisible(true);
        DownButton->Click += [&] (o, e)
        {
            if (CStartIndex + 22 >= CategoryList.size())
            {
                return;
            }
    
            CStartIndex++;
    
            SetCategories();
            UpdatePositionBar();
        };

        PositionBar = new MirButton();
        PositionBar->setIndex(205);
        PositionBar->setHoverIndex(206);
        PositionBar->setPressedIndex(206);
        PositionBar->setLibrary(Libraries::Prguse2);
        Point tempVar15(120, 117);
        PositionBar->setLocation(&tempVar15);
        PositionBar->setParent(this);
        PositionBar->setMovable(true);
        PositionBar->setSound(SoundList::None);
        PositionBar->setVisible(true);
        PositionBar->OnMoving->addListener("PositionBar_OnMoving", std::bind(&GameShopDialog::PositionBar_OnMoving, this, std::placeholders::_1, std::placeholders::_2));




        FilterBackground = new MirImageControl();
        FilterBackground->setIndex(769);
        FilterBackground->setLibrary(Libraries::Title);
        Point tempVar16(11, 102);
        FilterBackground->setLocation(&tempVar16);
        FilterBackground->setParent(this);
        FilterBackground->MouseWheel += FilterScrolling;

        Search = new MirTextBox();
        Search->setBackColour(Color::FromArgb(4, 4, 4));
        Search->setForeColour(Color::White);
        Search->setParent(this);
        Size tempVar17(140, 16);
        Search->setSize(&tempVar17);
        Point tempVar18(540, 69);
        Search->setLocation(&tempVar18);
        Font tempVar19(Settings::FontName, 9.0F);
        Search->setFont(&tempVar19);
        Search->setMaxLength(23);
        Search->CanLoseFocus = true;
        Search->TextBox->KeyUp += [&] (o, e)
        {
            GetCategories();
        };

        allItems = new MirButton();
        allItems->setIndex(770);
        allItems->setLibrary(Libraries::Title);
        Point tempVar20(138, 68);
        allItems->setLocation(&tempVar20);
        allItems->setVisible(true);
        allItems->setParent(this);
        allItems->setSound(SoundList::ButtonA);
        allItems->Click += [&] (o, e)
        {
            SectionFilter = "Show All";
            ResetTabs();
            GetCategories();
        };
        topItems = new MirButton();
        topItems->setIndex(776);
        topItems->setLibrary(Libraries::Title);
        Point tempVar21(209, 68);
        topItems->setLocation(&tempVar21);
        topItems->setVisible(true);
        topItems->setParent(this);
        topItems->setSound(SoundList::ButtonA);
        topItems->Click += [&] (o, e)
        {
            SectionFilter = "TopItems";
            ResetTabs();
            GetCategories();
        };
        Deals = new MirButton();
        Deals->setIndex(772);
        Deals->setLibrary(Libraries::Title);
        Point tempVar22(280, 68);
        Deals->setLocation(&tempVar22);
        Deals->setVisible(true);
        Deals->setParent(this);
        Deals->setSound(SoundList::ButtonA);
        Deals->Click += [&] (o, e)
        {
            SectionFilter = "DealItems";
            ResetTabs();
            GetCategories();
        };
        New = new MirButton();
        New->setIndex(774);
        New->setLibrary(Libraries::Title);
        Point tempVar23(351, 68);
        New->setLocation(&tempVar23);
        New->setVisible(false);
        New->setParent(this);
        New->setSound(SoundList::ButtonA);
        New->Click += [&] (o, e)
        {
            SectionFilter = "NewItems";
            ResetTabs();
            New->setIndex(775);
            GetCategories();
        };


        ALL = new MirButton();
        ALL->setIndex(751);
        ALL->setHoverIndex(752);
        ALL->setPressedIndex(753);
        ALL->setLibrary(Libraries::Title);
        Point tempVar24(539, 37);
        ALL->setLocation(&tempVar24);
        ALL->setVisible(true);
        ALL->setParent(this);
        ALL->Click += [&] (o, e)
        {
            ClassFilter = "Show All";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };
        War = new MirButton();
        War->setIndex(754);
        War->setHoverIndex(755);
        War->setPressedIndex(756);
        War->setLibrary(Libraries::Title);
        Point tempVar25(568, 38);
        War->setLocation(&tempVar25);
        War->setVisible(true);
        War->setParent(this);
        War->Click += [&] (o, e)
        {
            ClassFilter = "Warrior";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };
        Sin = new MirButton();
        Sin->setIndex(757);
        Sin->setHoverIndex(758);
        Sin->setPressedIndex(759);
        Sin->setLibrary(Libraries::Title);
        Point tempVar26(591, 38);
        Sin->setLocation(&tempVar26);
        Sin->setVisible(true);
        Sin->setParent(this);
        Sin->Click += [&] (o, e)
        {
            ClassFilter = "Assassin";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };
        Tao = new MirButton();
        Tao->setIndex(760);
        Tao->setHoverIndex(761);
        Tao->setPressedIndex(762);
        Tao->setLibrary(Libraries::Title);
        Point tempVar27(614, 38);
        Tao->setLocation(&tempVar27);
        Tao->setVisible(true);
        Tao->setParent(this);
        Tao->Click += [&] (o, e)
        {
            ClassFilter = "Taoist";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };
        Wiz = new MirButton();
        Wiz->setIndex(763);
        Wiz->setHoverIndex(764);
        Wiz->setPressedIndex(765);
        Wiz->setLibrary(Libraries::Title);
        Point tempVar28(637, 38);
        Wiz->setLocation(&tempVar28);
        Wiz->setVisible(true);
        Wiz->setParent(this);
        Wiz->Click += [&] (o, e)
        {
            ClassFilter = "Wizard";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };
        Arch = new MirButton();
        Arch->setIndex(766);
        Arch->setHoverIndex(767);
        Arch->setPressedIndex(768);
        Arch->setLibrary(Libraries::Title);
        Point tempVar29(660, 38);
        Arch->setLocation(&tempVar29);
        Arch->setVisible(true);
        Arch->setParent(this);
        Arch->Click += [&] (o, e)
        {
            ClassFilter = "Archer";
            TypeFilter = "Show All";
            GetCategories();
            ResetClass();
        };

        PageNumberLabel = new MirLabel();
        PageNumberLabel->setText("");
        PageNumberLabel->setParent(this);
        Size tempVar30(83, 17);
        PageNumberLabel->setSize(&tempVar30);
        Point tempVar31(597, 446);
        PageNumberLabel->setLocation(&tempVar31);
        PageNumberLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        Font tempVar32(Settings::FontName, 7.0F);
        PageNumberLabel->setFont(&tempVar32);

        PreviousButton = new MirButton();
        PreviousButton->setIndex(240);
        PreviousButton->setHoverIndex(241);
        PreviousButton->setPressedIndex(242);
        PreviousButton->setLibrary(Libraries::Prguse2);
        PreviousButton->setParent(this);
        Point tempVar33(600, 448);
        PreviousButton->setLocation(&tempVar33);
        PreviousButton->setSound(SoundList::ButtonA);
        PreviousButton->Click += [&] (o, e)
        {
            Page--;
            if (Page < 0)
            {
                Page = 0;
            }
            StartIndex = Grid.size() * Page;
    
            UpdateShop();
        };

        NextButton = new MirButton();
        NextButton->setIndex(243);
        NextButton->setHoverIndex(244);
        NextButton->setPressedIndex(245);
        NextButton->setLibrary(Libraries::Prguse2);
        NextButton->setParent(this);
        Point tempVar34(660, 448);
        NextButton->setLocation(&tempVar34);
        NextButton->setSound(SoundList::ButtonA);
        NextButton->Click += [&] (o, e)
        {
            Page++;
            if ((Page + 1) > maxPage)
            {
                Page--;
            }
            StartIndex = Grid.size() * Page;
            UpdateShop();
        };

        for (int i = 0; i < Filters.size(); i++)
        {
            MirLabel *tempVar35 = new MirLabel();
            tempVar35->setParent(this);
            Size tempVar36(90, 20);
            tempVar35->setSize(&tempVar36);
            Point tempVar37(15, 103 + (15 * i));
            tempVar35->setLocation(&tempVar37);
            tempVar35->setText("Testing - " + std::to_string(i));
            tempVar35->setForeColour(Color::Gray);
            Font tempVar38(Settings::FontName, 7.0F);
            tempVar35->setFont(&tempVar38);
            Filters[i] = tempVar35;
            Filters[i]->Click += [&] (o, e)
            {
                MirLabel *lab = static_cast<MirLabel*>(o);
                TypeFilter = lab->getText();
                Page = 0;
                StartIndex = 0;
                UpdateShop();
                for (int p = 0; p < Filters.size(); p++)
                {
                    if (Filters[p]->getText() == lab->getText())
                    {
                        Filters[p]->setForeColour(Color::FromArgb(230, 200, 160));
                    }
                    else
                    {
                        Filters[p]->setForeColour(Color::Gray);
                    }
                }
    
            };
            Filters[i]->MouseEnter += [&] (o, e)
            {
                MirLabel *lab = static_cast<MirLabel*>(o);
                for (int p = 0; p < Filters.size(); p++)
                {
                    if (Filters[p]->getText() == lab->getText() && Filters[p]->getForeColour() != Color::FromArgb(230, 200, 160))
                    {
                        Filters[p]->setForeColour(Color::FromArgb(160, 140, 110));
                    }
                }
            };
            Filters[i]->MouseLeave += [&] (o, e)
            {
                MirLabel *lab = static_cast<MirLabel*>(o);
                for (int p = 0; p < Filters.size(); p++)
                {
                    if (Filters[p]->getText() == lab->getText() && Filters[p]->getForeColour() != Color::FromArgb(230, 200, 160))
                    {
                        Filters[p]->setForeColour(Color::Gray);
                    }
                }
            };
            Filters[i]->MouseWheel += FilterScrolling;

            delete tempVar35;
        }

        Viewer = new GameShopViewer();

    }

    void GameShopDialog::Hide()
    {
        if (!getVisible())
        {
            return;
        }
        Viewer->setVisible(false);
        setVisible(false);
    }

    void GameShopDialog::Show()
    {
        if (getVisible())
        {
            return;
        }
        setVisible(true);
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        ClassFilter = GameScene::getUser()->Class.ToString();
        SectionFilter = "Show All";
        ResetTabs();
        ResetClass();
        GetCategories();
    }

    void GameShopDialog::Dispose(bool disposing)
    {
        MirImageControl::Dispose(disposing);

        Search->Dispose();
        Search = nullptr;

        PageNumberLabel = nullptr;
        totalGold = nullptr;
        totalCredits = nullptr;
        ALL = nullptr;
        War = nullptr;
        Sin = nullptr;
        Tao = nullptr;
        Wiz = nullptr;
        Arch = nullptr;
        allItems = nullptr;
        topItems = nullptr;
        Deals = nullptr;
        New = nullptr;

        CloseButton = nullptr;
        PreviousButton = nullptr;
        NextButton = nullptr;

        UpButton = nullptr;
        DownButton = nullptr;
        PositionBar = nullptr;

        Grid = std::vector<GameShopCell*>();
        Filters = std::vector<MirLabel*>();
        FilterBackground = nullptr;

        Viewer->Dispose();

        CategoryList.clear();
        filteredShop.clear();
        SearchResult.clear();
    }

    void GameShopDialog::Process()
    {
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        totalCredits->setText(GameScene::Credit.ToString("###,###,##0"));
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        totalGold->setText(GameScene::Gold.ToString("###,###,##0"));
    }

    void GameShopDialog::FilterScrolling(std::any sender, MouseEventArgs *e)
    {
        int count = e->Delta / SystemInformation::MouseWheelScrollDelta;

        if (CStartIndex == 0 && count >= 0)
        {
            return;
        }
        if (CStartIndex == CategoryList.size() - 1 && count <= 0)
        {
            return;
        }
        if (CategoryList.size() <= 22)
        {
            return;
        }

        CStartIndex -= count;

        if (CStartIndex < 0)
        {
            CStartIndex = 0;
        }
        if (CStartIndex + 22 > CategoryList.size() - 1)
        {
            CStartIndex = CategoryList.size() - 22;
        }

        SetCategories();

        UpdatePositionBar();

    }

    void GameShopDialog::UpdatePositionBar()
    {
        if (CategoryList.size() <= 22)
        {
            return;
        }

        int interval = 290 / (CategoryList.size() - 22);

        int x = 120;
        int y = 117 + (CStartIndex * interval);

        if (y >= 401)
        {
            y = 401;
        }
        if (y <= 117)
        {
            y = 117;
        }

        Point tempVar(x, y);
        PositionBar->setLocation(&tempVar);
    }

    void GameShopDialog::PositionBar_OnMoving(std::any sender, MouseEventArgs *e)
    {
        int x = 120;
        int y = PositionBar->getLocation()->Y;

        if (y >= 401)
        {
            y = 401;
        }
        if (y <= 117)
        {
            y = 117;
        }

        if (CategoryList.size() > 22)
        {
            int location = y - 117;
            int interval = 284 / (CategoryList.size() - 22);

            double yPoint = location / interval;

            CStartIndex = static_cast<short>(std::floor(yPoint));
            SetCategories();
        }

        Point tempVar(x, y);
        PositionBar->setLocation(&tempVar);
    }

    void GameShopDialog::ResetTabs()
    {
        allItems->setIndex(770);
        topItems->setIndex(776);
        Deals->setIndex(772);
        New->setIndex(774);

        if (SectionFilter == "Show All")
        {
            allItems->setIndex(771);
        }
        if (SectionFilter == "TopItems")
        {
            topItems->setIndex(777);
        }
        if (SectionFilter == "DealItems")
        {
            Deals->setIndex(773);
        }
        if (SectionFilter == "NewItems")
        {
            New->setIndex(775);
        }
    }

    void GameShopDialog::ResetClass()
    {
        ALL->setIndex(751);
        War->setIndex(754);
        Sin->setIndex(757);
        Tao->setIndex(760);
        Wiz->setIndex(763);
        Arch->setIndex(766);

        if (ClassFilter == "Show All")
        {
            ALL->setIndex(752);
        }
        if (ClassFilter == "Warrior")
        {
            War->setIndex(755);
        }
        if (ClassFilter == "Assassin")
        {
            Sin->setIndex(758);
        }
        if (ClassFilter == "Taoist")
        {
            Tao->setIndex(761);
        }
        if (ClassFilter == "Wizard")
        {
            Wiz->setIndex(764);
        }
        if (ClassFilter == "Archer")
        {
            Arch->setIndex(767);
        }
    }

    void GameShopDialog::GetCategories()
    {
        TypeFilter = "Show All";
        Page = 0;
        StartIndex = 0;
        std::vector<GameShopItem*> shopList;

        if (Search->TextBox->Text != "")
        {
            shopList = GameScene::GameShopInfoList.Where([&] (std::any f)
            {
                return f::Info::FriendlyName->ToLower().Contains(Search->TextBox->Text.ToLower());
            }).ToList();
        }
        else
        {
            shopList = GameScene::GameShopInfoList;
        }

        CategoryList.clear();
        Point tempVar(120, 117);
        PositionBar->setLocation(&tempVar);
        CategoryList.push_back("Show All");

        for (int i = 0; i < shopList.size(); i++)
        {
            if (!std::find(CategoryList.begin(), CategoryList.end(), shopList[i]->Category) != CategoryList.end() && shopList[i]->Category != "")
            {
                if (shopList[i]->Class == ClassFilter || shopList[i]->Class == "All" || ClassFilter == "Show All")
                {
                    if (SectionFilter == "Show All" || SectionFilter == "TopItems" && shopList[i]->TopItem || SectionFilter == "DealItems" && shopList[i]->Deal || SectionFilter == "NewItems" && shopList[i]->Date > DateTime::Now.AddDays(-7))
                    {
                        CategoryList.push_back(shopList[i]->Category);
                    }
                }

            }
        }
        Filters[0]->setForeColour(Color::FromArgb(230, 200, 160));
        CStartIndex = 0;
        SetCategories();
        UpdateShop();
    }

    void GameShopDialog::SetCategories()
    {
        for (int i = 0; i < Filters.size(); i++)
        {
            if (i < CategoryList.size())
            {
                Filters[i]->setText(CategoryList[i + CStartIndex]);
                Filters[i]->setForeColour(Filters[i]->getText() == TypeFilter ? Color::FromArgb(230, 200, 160) : Color::Gray);
                Filters[i]->setNotControl(false);
            }
            else
            {
                Filters[i]->setText("");
                Filters[i]->setNotControl(true);
            }
        }

    }

    void GameShopDialog::UpdateShop()
    {
        std::vector<GameShopItem*> ShopList;

        if (Search->TextBox->Text != "")
        {
            ShopList = GameScene::GameShopInfoList.Where([&] (std::any f)
            {
                return f::Info::FriendlyName->ToLower().Contains(Search->TextBox->Text.ToLower());
            }).ToList();
        }
        else
        {
            ShopList = GameScene::GameShopInfoList;
        }

        for (int i = 0; i < Grid.size(); i++)
        {
            if (Grid[i] != nullptr)
            {
                Grid[i]->Dispose();
            }
            Grid[i]->Item = nullptr;
        };


        filteredShop.clear();

        for (int i = 0; i < ShopList.size(); i++)
        {
            if (ShopList[i]->Class == ClassFilter || ShopList[i]->Class == "All" || ClassFilter == "Show All")
            {
                if (ShopList[i]->Category == TypeFilter || TypeFilter == "Show All")
                {
                    if (SectionFilter == "Show All" || SectionFilter == "TopItems" && ShopList[i]->TopItem || SectionFilter == "DealItems" && ShopList[i]->Deal || SectionFilter == "NewItems" && ShopList[i]->Date > DateTime::Now.AddDays(-7))
                    {
                        filteredShop.push_back(ShopList[i]);
                    }
                }
            }
        }


        maxPage = (static_cast<Decimal>(filteredShop.size()) / 8);
        maxPage = std::ceil(maxPage);
        if (maxPage < 1)
        {
            maxPage = 1;
        }

        PageNumberLabel->setText(std::to_string(Page + 1) + " / " + std::to_string(maxPage));

        int maxIndex = filteredShop.size() - 1;

        if (StartIndex > maxIndex)
        {
            StartIndex = maxIndex;
        }
        if (StartIndex < 0)
        {
            StartIndex = 0;
        }

        filteredShop = filteredShop.OrderBy([&] (std::any e)
        {
            e::Info::FriendlyName;
        }).ToList();

        for (int i = 0; i < Grid.size(); i++)
        {
            if (i + StartIndex >= filteredShop.size())
            {
                break;
            }

            if (Grid[i] != nullptr)
            {
                Grid[i]->Dispose();
            }

            GameShopCell *tempVar = new GameShopCell();
            tempVar->setVisible(true);
            tempVar->Item = filteredShop[i + StartIndex];
            Size tempVar2(125, 146);
            tempVar->setSize(&tempVar2);
            Point tempVar3(152 + ((i - 4) * 132), 275);
            tempVar->setLocation(i < 4 ? new Point(152 + (i * 132), 115) : &tempVar3);
            tempVar->setParent(this);
            Grid[i] = tempVar;

            delete tempVar;
        }

        GameScene::Scene->GameShopDialog->Viewer->setVisible(false);

    }
}
